<?php
class UsersController extends AppController {
	var $name = 'Users';
	var $components = array('Auth'); //No es necesario si se declaro en el app controller
	var $helpers = array('Html', 'Session','Javascript');
	

	
	/**
	 * El AuthComponent proporciona la funcionalidad necesaria
	 * para el acceso (login), por lo que se puede dejar esta funcion en blanco.
	 */
	
	function beforeFilter(){
		$this->layout = 'admin';
		$this->Auth->loginError = "Usuario o contraseña incorrectos.";
		$this->Auth->authError = "Debes estar logeado para poder acceder a la zona de administración.";
	}
	
	function login() {
		
	}
	function logout() {
		$this->redirect($this->Auth->logout());
	}
	function products() {
		
	}
	function images(){
		
	}
	function borrar(){
		echo "Borrando...";
		if (isset($this->params['pass'][0])){
			
			App::import('Model','Product');
			$product = new Product;
			App::import('Model','Image');
			$image = new Image;
			
			$ruta = "img/upload/";
			$images = ClassRegistry::init('Image')->find('all', Array('conditions' => Array('Image.product_id' => $this->params['pass'][0])));
			
			foreach($images as $img){
				echo "<br/>";
				echo $img['Image']['url_full'];
				echo $img['Image']['url_thumbnail'];
				unlink($ruta.$img['Image']['url_full']);
				unlink($ruta.$img['Image']['url_thumbnail']);
			}
			
			$product->deleteAll(Array('Product.id' =>  $this->params['pass'][0]));
			$image->deleteAll(Array('product_id' =>  $this->params['pass'][0]));
			echo "Todo hecho, nos vamos a ir <br/>";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=http://www.moblesalcaraz.es/users/products'>";
			//$this->redirect(Array('controller' => 'users', 'action' => 'products'));
			//Es sucio, pero una solucion provisional hasta encontrar otra cosa

			
			
		}else {
			$this->redirect(Array('controller' => 'users', 'action' => 'products'));
		}
	}
	
	function borrarSubcat(){
		/*echo $this->params['pass'][0];
		App::import('Model','Subcategory');
		$subcategory = new Subcategory;
		App::import('Model','Product');
		$product = new Product;
		App::import('Model','Image');
		$image = new Image;
		$producstId = ClassRegistry::init('Product')->find('all', Array('conditions' => Array('Product.subcategory_id' => $this->params['pass'][0])));
		//print_r($producstId);
			
		foreach($producstId as $producto){
		echo $producto['Product']['id'];
		$image->deleteAll(Array('id' =>  $producto['Product']['id']));
		}
			
		$subcategory->deleteAll(Array('id' =>  $this->params['pass'][0]), true);
			
		$product->deleteAll(Array('subcategory_id' =>  $this->params['pass'][0]), true);*/
	}
	
		
}?>
